require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIDuplicationDetails = {};
HelperFunc.SetUIEnv(UIDuplicationDetails)
local this = UIDuplicationDetails;

this.hasInit = false;

this.btnBackButton = nil;
this.txtTitle = nil;
this.objHeroContent = nil;
this.objItemContent = nil;
this.btnRecord = nil;

this.imgTactics = nil;
this.txtTactics = nil;
this.objRestrain = nil;
this.txtRestrain = nil;

this.blackBtn = nil;

this.tableHeroItems ={};
this.tableItemItems ={};
this.tableHeroItemItems = {};
this.oppoList = {};
this.itemDropInfos = {};
this.heroDropInfos = {};
this.rewardInfo = {};


function UIDuplicationDetails.Init()--todo 应该小写 暂时先大写
    local name, packageName = getUIName(PanelResNames.UIDuplicationDetails);
    panelMgr:Push(name, packageName, this.onCreate,Config.useLocalUI);
end

function UIDuplicationDetails.onCreate(obj)
    
    if not hasInit then
        hasInit = true;
        blackBtn = obj:GetChild("blackBtn")
        btnBackButton = obj:GetChild("bg")
        txtTitle = obj:GetChild("title")
        objHeroContent = obj:GetChild("HeroSv")
        objItemContent = obj:GetChild("ItemSv")
        btnRecord = obj:GetChild("Button")
        
        imgTactics = obj:GetChild("TacticsIcon")
        txtTactics = obj:GetChild("TacticsName")
        objRestrain = obj:GetChild("RestrainIcon")
        txtRestrain = obj:GetChild("RestrainName")
        this.addEvent();
    end
    --panelMgr.ShowFadeEff(transform);
    this.refresh();
end

function UIDuplicationDetails.addEvent()
    blackBtn.onClick:Add(this.onClickClose)
    btnBackButton.onClick:Add(this.onClickClose)
    btnRecord.onClick:Add(this.onClickRecord)

    --LuaBehaviour:AddClick(btnBackButton.gameObject, this.onClickClose);
    --LuaBehaviour:AddClick(btnRecord.gameObject, this.onClickRecord);

end

function UIDuplicationDetails.removeEvent()
    blackBtn.onClick:Remove(this.onClickClose)
    btnBackButton.onClick:Remove(this.onClickClose)
    btnRecord.onClick:Remove(this.onClickRecord)

    --LuaBehaviour:RemoveClick(btnBackButton.gameObject);
    --LuaBehaviour:RemoveClick(btnRecord.gameObject);

end

function UIDuplicationDetails.LocalListenEvent()

end

function UIDuplicationDetails.LocalCloseEvent()

end

function UIDuplicationDetails.OnDestroy()
    this.removeEvent();
end

function UIDuplicationDetails.OnEnable()
    this.LocalListenEvent();
end

function UIDuplicationDetails.OnDisable()
    this.LocalCloseEvent();
end


function UIDuplicationDetails.refresh()
    local mapConfig = logicMgr.MapManager.GetMapcfgByID(logicMgr.MapManager.getMapId());
    if mapConfig.f_NextStage~=nil and mapConfig.f_NextStage>0 then
        mapConfig = logicMgr.MapManager.GetMapcfgByID(mapConfig.f_NextStage);
    end

    local MonsterList = decodeJsonStrToTabel(mapConfig.f_MonsterTeamId);
    local oppoTeamConfig = dataTable.getDataTableByStringId("t_monsterteam",MonsterList[1])

    txtTitle.text = mapConfig.f_ChapterId.."-"..mapConfig.f_StageNum;

    oppoList = {};
    if oppoTeamConfig~=nil then
        for i = 1, 6 do
            if oppoTeamConfig["f_MonsterPara"..i]~=nil then
                table.insert(oppoList,decodeJsonStrToTabel(oppoTeamConfig["f_MonsterPara"..i]));
            end
        end
    end

    --地方阵容
    objHeroContent.itemRenderer = this.setData;
    objHeroContent.numItems = #oppoList;

    --通关奖励
    itemDropInfos = decodeJsonStrToTabel(mapConfig.f_StageReward);
    heroDropInfos = decodeJsonStrToTabel(mapConfig.f_StageRewardHero);
    if heroDropInfos == nil then
        heroDropInfos = {};
    end
    if itemDropInfos == nil then
        itemDropInfos = {};
    end
    for i = 1, #itemDropInfos do
        table.insert(rewardInfo,itemDropInfos[i]);
    end
    for j = 1, #heroDropInfos do
        table.insert(rewardInfo,heroDropInfos[j]);
    end

    objItemContent.itemRenderer = this.rewardSetData;
    objItemContent.numItems = #itemDropInfos;

    if oppoTeamConfig~=nil then
        
        local oppoTacticsRealId = oppoTeamConfig.f_TacticsId_Id~= nil and  oppoTeamConfig.f_TacticsId_Id or 0;
        local curTactics = logicMgr.TacticsManager.getCurTactics();
        local oppoTacticsConfig=oppoTacticsRealId>0 and logicMgr.TacticsManager.getTacticsConfigByUid(oppoTacticsRealId) or nil;

        if curTactics == nil and oppoTacticsConfig == nil then
            txtRestrain.text = getLanguage("Sys_EmblemCounterType3");
        elseif curTactics == nil and oppoTacticsConfig~=nil then
            txtRestrain.text = getLanguage("Sys_EmblemCounterType4");
            objRestrain.url = "ui://UIDuplicationDetails/lose1";
            --objRestrain.transform:Find("lose1").gameObject:SetActive(true);
        else
            local flag = false;
            local oppoTacticsId = oppoTacticsConfig~=nil and oppoTacticsConfig.f_TacticsId or 0;
            for i = 1, #curTactics.restrains do
                if curTactics.restrains[i].tactics == oppoTacticsId then
                    flag =true;
                    if curTactics.restrains[i].type == 1 then
                        objRestrain.url = "ui://UIDuplicationDetails/win1";
                        --objRestrain.transform:Find("win1").gameObject:SetActive(true);
                        txtRestrain.text = getLanguage("Sys_EmblemCounterType2");
                    elseif curTactics.restrains[i].type == 2 then
                        objRestrain.url = "ui://UIDuplicationDetails/win2";
                        --objRestrain.transform:Find("win2").gameObject:SetActive(true);
                        txtRestrain.text = getLanguage("Sys_EmblemCounterType1");
                    end
                    break;
                end
            end

            if not flag then
                for i = 1, #curTactics.berestrains do
                    if curTactics.berestrains[i].tactics == oppoTacticsId then
                        flag =true;
                        if curTactics.berestrains[i].type == 1 then
                            objRestrain.url = "ui://UIDuplicationDetails/lose1";
                            --objRestrain.transform:Find("lose1").gameObject:SetActive(true);
                            txtRestrain.text = getLanguage("Sys_EmblemCounterType4");
                        elseif curTactics.berestrains[i].type == 2 then
                            objRestrain.url = "ui://UIDuplicationDetails/lose2";
                            --objRestrain.transform:Find("lose2").gameObject:SetActive(true);
                            txtRestrain.text = getLanguage("Sys_EmblemCounterType5");
                        end
                        break;
                    end
                end
            end

            if not flag then
                txtRestrain.text = getLanguage("Sys_EmblemCounterType3");
            end
        end
    else
        imgTactics.visible = false;
        txtTactics.visible = false;
        objRestrain.visible = false;
        txtRestrain.visible = false;
    end

end

function UIDuplicationDetails.setData(index,obj)
    local heroConfig = logicMgr.HeroManager.GetHeroConfigbyModelId(oppoList[index + 1][1]);
    local data = {
        level = oppoList[index + 1][2],
        star = logicMgr.HeroManager.starToShow(oppoList[index + 1][4]),
        icon = heroConfig.f_HeroIcon,
        quality = logicMgr.HeroManager.starToQuality(oppoList[index + 1][4]),
        state = EnumConst.HeroHeadStateEnum.NONE,
        type = heroConfig.f_HeroType
    };
    local itemCtr = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
    itemCtr.initData(obj,data);
end
 
function UIDuplicationDetails.rewardSetData(index,obj)
    local data = itemDropInfos[index + 1];
    -- if index <= #itemDropInfos then
    local itemConfig = logicMgr.ItemManager.getItemConstDatabyModelId(data[1]);
    if itemConfig == nil then
        obj.visible = false;
        return;
    end
    itemConfig.num =  data[2];
    local itemCtr = CtrlManager.GetCtrl(PanelNames.Item);
    itemCtr.initData(obj,itemConfig);
    obj.onClick:Clear();
    obj.onClick:Add(function()
        this.onClickItem(itemConfig);
    end);
    -- else
    --     local heroConfig = logicMgr.HeroManager.GetHeroConfigbyModelId(itemDropInfos[index][1]);
    --     local data = {
    --         level = 1,
    --         star = heroConfig.star,
    --         icon = heroConfig.f_HeroIcon,
    --         quality = logicMgr.HeroManager.starToQuality(heroConfig.f_HeroInitialStar),
    --         state = EnumConst.HeroHeadStateEnum.NONE
    --     };
    --     local itemCtr = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
    --     itemCtr.initData(obj,data);
    -- end
end

function UIDuplicationDetails.onClickClose()
    closeUI(PanelNames.UIDuplicationDetails);
end

function UIDuplicationDetails.onClickRecord()
    local mapConfig = logicMgr.MapManager.GetMapcfgByID(logicMgr.MapManager.getMapId())
    showUI(PanelNames.UILevelStrategy,EnumConst.FightTypeConst.DUNGEON,mapConfig.f_StageId)
end

function UIDuplicationDetails.onClickItem(itemData)
    showUI(PanelNames.UIBagItemInfo,itemData,1);
end

return UIDuplicationDetails;
